<template>
	<view class="discount">
		<cu-custom bgColor="bg-gradual-orange-red" :isBack="true">
			<block slot="content">优惠券管理</block>
		</cu-custom>
		
		<view class="main-content">
			<!-- <view class="tab-label">
				<view class="tab-item" :class="[active?'onActive':'']">
					<text class="item-title" @click="changeType('1')">发放中</text>
				</view>
				<view class="tab-item" :class="[active?'':'onActive']">
					<text class="item-title" @click="changeType('0')">已发放</text>
				</view>
			</view> -->
			<view class="discount-list" v-if="discount">
				<view class="list-item radius shadow bg-white" v-for="items in discount" :key="items.scid">
					<view class="item-header">
						<view class="item-name item-left">优惠券 - {{items.title}}</view>
						<view class="item-status item-right" :class="[items.status==1?'onTake':'']" v-if="items.status">发放中</view>
						<view class="item-status item-right" :class="[items.status==0?'':'onTake']" v-else>已发完</view>
					</view>
					<view class="item-middle">
						<view class="item-value item-left">{{items.coupon_price}} <text class="text-icon"> 元</text></view>
						<view class="item-switch item-right">
							<view v-if="items.status">
								<button class="cu-btn round bg-olive takeTurn" @click="changStatus(items)">领取</button>
							</view>
							<!-- <button class="cu-btn round bg-red takeStop" v-if="items.status" @click="changStatus(items)">暂停</button> -->
							<!-- <button class="cu-btn round bg-red" v-else @click="delItems(items)">删除该券</button> -->
						</view>
					</view>
					<view class="item-footer">
						<view class="item-base item-left">
							<view class="item-threshold ">满 {{items.use_min_price}}  可用</view>
							<view class="item-userful ">截止日期：{{items.coupon_time_text}} </view>
						</view>
						<view class="item-count item-right" v-if="items.is_permanent">
							<view class="items-complete "><text class="text-red">无限张</text></view>
						</view>
						<view class="item-count item-right" v-else>
							<view class="items-complete ">已发完 <text class="text-red">{{items.total_count-items.remain_count}}</text> 张</view>
							<view class="items-surplus ">剩余 <text class="text-green">{{items.remain_count}}</text> 张</view>
						</view>
					</view>
				</view>
				<uni-load-more :status="more" v-if="discount"></uni-load-more>
			</view>
			<view class="discount-list" v-else>
			</view>
		</view>
		<view class="invite-tips" v-if="tipStatus">
			<view class="invite-body">
				<view class="invite-title">提示</view>
				<view class="invite-content">去微信分享该优惠券</view>
				<view class="invite-btn">
					<button plain="true" class="takeCencel" @click="shareFriends">取消</button>
					<button plain="true" class="takeConfirms" open-type="share" @click="shareFriends">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	import shops from '@/api/shop';
	export default {
		data() {
			return {
				share: {
					path: '/pages/coupon/index',
					title: '满减券',
					imageUrl: '/static/share.png'
				},
				more: 'more',
				page:1,
				limit:10,
				status:1,
				shareId:'',
				discount:[],
				onShow:1,
				active:true,
				tipStatus: false,
			}
		},
		components: {
			uniLoadMore
		},
		onShow() {
			// this.getRecord(this.status);
		},
		onLoad() {
			this.getRecord(this.status);
		},
		onReachBottom() {
			if (this.more !== 'noMore') {
				this.more = 'loading'
				this.page++;
				this.getRecord(this.status);
			}
		},
		methods: {
			shareFriends(option) {
				if(option.status==0){
					this.shareId = '';
					uni.showToast({
						title:'优惠券失效，无法转发',
						icon:false,
					})
					return false;
				}
				this.shareId = option.scid;
				this.tipStatus = !this.tipStatus;
			},
			onShareAppMessage(res){
				console.log('id==='+this.shareId);
				return {
					path: this.share.path +'?id='+this.shareId,
					title: this.share.title,
					imageUrl: this.share.imageUrl
				}
			},
			delItems(items){
				if(!items){
					uni.showToast({
						title:'网络错误...',
						icon:false,
					})
					return false;
				}
				shops.delShopCoupon({id:items.scid}).then(res=>{
					if(res.code ==1){
						uni.showToast({
							title:'已删除',
						})
						this.getRecord(this.status);
					}else{
						uni.showToast({
							title:'网络错误...',
							icon:false,
						})
						return false;
						console.log(res);
					}
				})
			},
			changStatus(param){
				// this.status = this.status?'0':'1';
				// if(!param){
				// 	uni.showToast({
				// 		title:'网络错误...',
				// 		icon:false,
				// 	})
				// 	return false;
				// }
				param.id = param.scid;
				param.status = param.status?'0':'1';
				param.coupon_time = param.coupon_time_text;
				
				shops.drawShopCoupon(param).then(res => {
					let _this = this;
					uni.hideLoading();
					if(res.code == 1){
						uni.showModal({
							title:'提示',
							content:res.msg,
							showCancel:false,
							success:function(res){
								_this.getRecord(_this.status);
							}
						})
					}else{
						uni.showModal({
							title:'提示',
							content:res.msg,
							icon:false,
						})
						console.log(res);
					}
				})
			},
			changeType(cate){
				this.active = !this.active;
				this.page = 1;
				this.discount=[];
				this.getRecord(cate);
			},
			getRecord(type){
				uni.showLoading({
					title:'加载中...'
				});
				let _this = this;
				shops.AllCouponList({page:this.page,limit:this.limit,status:type}).then(res =>{
					uni.hideLoading();
					if(res.code == 1){
						if (res.data.length <= 0) {
							_this.more = 'noMore';
						} else {
							_this.more = 'more';
							if(_this.discount.length>0){
								_this.discount = res.data[0].status == _this.discount[0].status?_this.discount.concat(res.data):res.data
							}else{
								_this.discount = type?res.data:_this.discount.concat(res.data)
							}
						}
						
					}else{
						console.log(res);
					}
				})
			},
			
		}
	}
</script>

<style lang="less">
	.discount{
		font-family: 'SimHei';
		font-size: 28rpx;
		background-color: #eeeeee;
		color: #999999;
		.top-header{
			.top-banner{
			    position: relative;
			    height: 175rpx;
				image{
				    height: 175rpx;
				    width: 100%;
				}
				.top-title{
				    position: absolute;
				    z-index: 2;
				    top: 0;
				    height: 175rpx;
				    line-height: 175rpx;
				    width: 100%;
				    text-align: center;
				    color: #fff;
				    font-size: 35rpx;
				}
			}
			
		}
		.main-content{
			.tab-label{
			    height: 110rpx;
			    color: #999999;
				background-color: #ffffff;
			}
			.tab-item{
				width: 50%;
				float: left;
				height: 110rpx;	
				font-size: 32rpx;
				text-align: center;
				text{
					height: 100rpx;
					display: inline-block;
					line-height: 100rpx;
				}
			}
			.onActive{
				text{
					border-bottom: 5rpx solid red;
					color: red;
				}
			}
			.discount-list{
			    padding: 25rpx;
				.list-item{
					background-color: #fff;
				    border-radius: 10rpx;
				    font-size: 30rpx;
				    margin-bottom: 15rpx;
					.item-left{
						float: left;
					}
					.item-right{
						float: right;
						text-align: right;
					}
					.item-switch{
						padding: 15rpx 0;
					}
					.item-header{
					    height: 60rpx;
						padding-left:15rpx;
						view{
						    width: 50%;
						    height: 60rpx;
						    line-height: 60rpx;
						}
						.item-status{
						    width: 180rpx;
						    padding-right: 15rpx;
						    border-top-right-radius: 15rpx;
						    background-color: #8799a3;
						    text-align: center;
							color: #ffffff;
							border-bottom-left-radius: 15rpx;
						}
						.onTake{
							background-color: #e1251b;
						}
					}
					.item-middle{
						height: 150rpx;
						padding: 0 15rpx;
						.item-value{
						    color: #e1251b;
						    font-size: 70rpx;
						    font-weight: bold;
						    height: 150rpx;
							line-height: 150rpx;
						}
						.text-icon{
						    font-size: 28rpx;
						    padding-left: 6rpx;
						}
						.takeStop{
							background-color: #e54d42;
						    color: #ffffff;
							height: 55rpx;
							line-height: 55rpx;
						}
						.takeTurn{
							background-color: #f37b1d;
						    color: #ffffff;
							height: 55rpx;
							line-height: 55rpx;
							margin-bottom: 10rpx;
						}
					}
					.item-footer{
						padding: 0 15rpx;
					    height: 80rpx;
					    font-size: 25rpx;
					}
				}
			}
		}
	.invite-tips {
		position: fixed;
		top: 0;
		width: 100vw;
		height: 100vh;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.7);
		font-family: 'SimHei';
		font-size: 40rpx;
		color: #333333;
		text-align: center;
	
		.invite-body {
			position: absolute;
			width: 500rpx;
			left: 125rpx;
			height: 308rpx;
			top: 50%;
			margin-top: -154rpx;
			background-color: #ffffff;
			-webkit-border-radius: 5px;
			border-radius: 30rpx;
	
			.invite-title {
				padding-top: 50rpx;
				font-size: 34rpx;
				font-weight: 600;
			}
	
			.invite-content {
				height: 120rpx;
				line-height: 120rpx;
				font-size: 30rpx;
				color: rgb(65, 138, 110);
				border-bottom: 1rpx solid rgb(200, 230, 225);
			}
	
			.invite-btn {
				height: 80rpx;
				line-height: 80rpx;
				margin-top: 10rpx;
	
				button {
					width: 249.5rpx;
					display: inline-block;
					margin: 0;
					height: 80rpx;
					line-height: 80rpx;
					padding: 0;
					border: 0;
					border-radius: 0;
				}
	
				.takeCencel {
					border-right: 1rpx solid rgb(200, 230, 225);
					color: #333333;
				}
	
				.takeConfirms {
					color: #1CBBB4;
				}
			}
		}
	}
	}

</style>
